﻿using System;
using System.Threading;
using TYBase;
using TYRTUBase.Comm;

namespace TYRTUBase
{
    public enum EnumIRMeasSpd
    {
        EXFAST, FAST, MEDIUM, SLOW,
    }

    public class XMode_IR_Para
    {
        public double OCVMin { get; set; }
        public double OCVMax { get; set; }
        public double IRMin { get; set; }
        public double IRMax { get; set; }
    }

    public class Device_IR : DeviceBase
    {
        internal XMode_IR_Para XMode_IR_Para = new XMode_IR_Para()
        {
            OCVMin = 0,
            OCVMax = 10,
            IRMin = 0,
            IRMax = 100,
        };

        public void SetXModePara(XMode_IR_Para _XMode_IR_Para)
        {
            XMode_IR_Para.IRMin = _XMode_IR_Para.IRMin;
            XMode_IR_Para.IRMax = _XMode_IR_Para.IRMax;
            XMode_IR_Para.OCVMin = _XMode_IR_Para.OCVMin;
            XMode_IR_Para.OCVMax = _XMode_IR_Para.OCVMax;
        }


        public virtual bool DataToIR(string data, out double ocv, out double ir)
        {
            var data_tmp = data.Split(',');
            if (data_tmp.Length != 2)
            {
                ocv = 0;
                ir = 0;
                return false;
            }
            ir = Math.Abs(data_tmp[0].AsDouble()) * 1000;
            ocv = Math.Abs(data_tmp[1].AsDouble()) * 1000;
            return true;
        }

        public Device_IR(string equipId, string equipName, bool isSysRtu = false) : base(EnumEquipType.IR, equipId, equipName, isSysRtu)
        {
            ReadOutTime = 2000;
        }

        public EnumIRMeasSpd Spd { internal set; get; }


        public virtual bool InitIR(EnumIRMeasSpd irSpd)
        {
            Log.I("开始初始化IR串口");
            Spd = irSpd;
            if (IsXModerMode)
            {
                return true;
            }
            if (!CheckComOK())
            {
                Log.I($"IR串口未初始化或者打开{Com}");
                return false;
            }
            return true;
        }



        public virtual bool ReadData(out string data)
        {
            data = "";
            return true;
        }

        public void Init(int com) {
            para.Com = com;
            Init();
        }
    }
}